package com.lw.service.impl;

import com.lw.common.enums.GlobalTypeEnum;
import com.lw.entity.TbMenu;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.lw.entity.TbRoleMenu;
import com.lw.mapper.TbRoleMenuMapper;
import com.lw.service.TbRoleMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import static com.lw.entity.table.TbMenuTableDef.TB_MENU;
import static com.lw.entity.table.TbRoleMenuTableDef.TB_ROLE_MENU;
import static com.lw.entity.table.TbRoleTableDef.TB_ROLE;

/**
 * 角色菜单关系 服务层实现。
 *
 * @author lw
 * @since 2024年12月08日 07:53:39
 */
@Service
public class TbRoleMenuServiceImpl extends ServiceImpl<TbRoleMenuMapper, TbRoleMenu> implements TbRoleMenuService {

    private static final Logger logger = LoggerFactory.getLogger(TbRoleMenuServiceImpl.class);

    /**
     * 获取角色权限
     * by lw on 二〇二五年五月十三日 23:55:38
     *
     * @param role 角色集合
     */
    @Override
    public Set<String> getRolePerms(Set<Long> role) {
        List<TbMenu> tbMenusList = queryChain()
                .select(TB_MENU.PERM)
                .from(TB_ROLE_MENU)
                .join(TB_ROLE).on(TB_ROLE_MENU.ROLE_ID.eq(TB_ROLE.ID))
                .and(TB_ROLE.IS_DELETED.eq(GlobalTypeEnum.STATE_NO_DELETE))  //角色未删除状态
                .and(TB_ROLE.STATUS.eq(GlobalTypeEnum.STATE_ENABLE.getValue()))  //角色启用状态
                .and(TB_ROLE.ID.in(role))
                .join(TB_MENU).on(TB_ROLE_MENU.MENU_ID.eq(TB_MENU.ID)).listAs(TbMenu.class);
        return tbMenusList.stream().filter(Objects::nonNull).map(TbMenu::getPerm).collect(Collectors.toSet());

    }

    @Override
    public List<Long> getMenuIdsByRoleId(Long roleId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(TB_ROLE_MENU.ROLE_ID.getName(), roleId);
        List<TbRoleMenu> list = list(queryWrapper);
        if (list != null && !list.isEmpty()) {
            return list.stream().map(TbRoleMenu::getMenuId).toList();
        }
        return List.of();
    }

    @Override
    public boolean updateRoleMenuByRoleId(Long roleId, List<Long> menuIds) {
        // 删除角色的所有菜单关联
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(TB_ROLE_MENU.ROLE_ID.getName(), roleId);
        boolean remove = remove(queryWrapper);
        logger.info("删除角色菜单关联结果：{}", remove);

        // 插入新的菜单关联
        List<TbRoleMenu> tbRoleMenuList = new ArrayList<>();
        menuIds.forEach(menuId -> {
            TbRoleMenu tbRoleMenu = new TbRoleMenu();
            tbRoleMenu.setMenuId(menuId);
            tbRoleMenu.setRoleId(roleId);
            tbRoleMenuList.add(tbRoleMenu);
        });
        boolean b = saveBatch(tbRoleMenuList);
        logger.info("插入角色菜单关联结果：{}", b);
        return true;
    }

    @Override
    public List<String> getRolePermsList(Set<Long> role) {
        List<TbMenu> tbMenusList = queryChain()
                .select(TB_MENU.PERM)
                .from(TB_ROLE_MENU)
                .join(TB_ROLE).on(TB_ROLE_MENU.ROLE_ID.eq(TB_ROLE.ID))
                .and(TB_ROLE.IS_DELETED.eq(GlobalTypeEnum.STATE_NO_DELETE))  //角色未删除状态
                .and(TB_ROLE.STATUS.eq(GlobalTypeEnum.STATE_ENABLE.getValue()))  //角色启用状态
                .and(TB_ROLE.ID.in(role))
                .join(TB_MENU).on(TB_ROLE_MENU.MENU_ID.eq(TB_MENU.ID)).listAs(TbMenu.class);
        return tbMenusList.stream().filter(Objects::nonNull).map(TbMenu::getPerm).collect(Collectors.toList());
    }
}
